﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


using MECopter.Configuration;
namespace MECopter.Configuration.GroundControl
{
    /// <summary>
    /// Interaktionslogik für SerialConfigUI.xaml
    /// </summary>
    public partial class OptionsUI : Window
    {
        GeneralConfig general;
        SerialConfig serial;

        public OptionsUI()
        {
            InitializeComponent();            
            foreach(string port in System.IO.Ports.SerialPort.GetPortNames())
            {
                cbPort.Items.Add(port);
            }

            LoadConfig(ConfigType.Serial);
        }

        private void LoadConfig(ConfigType type)
        {
            switch (type)
            {
                case ConfigType.Serial:
                    serial = ConfigurationManager.CurrentSerialConfig;
                    cbPort.Text = serial.Port;
                    cbBaudrate.Text = serial.Baudrate.ToString();
                    tb_SerialSendInterval.Text = serial.SendInterval.ToString();
                    break;
            }
        }

        private void btSaveGeneral_Click(object sender, RoutedEventArgs e)
        {
            general = new GeneralConfig();
            general.ShowConsole = (bool)cbShowConsole.IsChecked;
            ConfigurationManager.Save(general);
        }

        private void btLoadGeneral_Click(object sender, RoutedEventArgs e)
        {
            LoadConfig(ConfigType.General);
        }

        private void btSaveSerial_Click(object sender, RoutedEventArgs e)
        {
            serial = new SerialConfig();
            serial.Port = cbPort.Text;
            serial.Baudrate = Convert.ToInt32(cbBaudrate.Text);
            serial.SendInterval = Convert.ToInt32(tb_SerialSendInterval.Text);
            ConfigurationManager.Save(serial);
        }

        private void btLoadSerial_Click(object sender, RoutedEventArgs e)
        {
            LoadConfig(ConfigType.Serial);
        }





        private void btLoadGeneral_Click_1(object sender, RoutedEventArgs e)
        {

        }
    }
}
